<template>
  <div>
    <el-form :model="formData" :rules="rules">
      <el-form-item prop="mobile" label="手机号码">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item prop="password" label="password">
        <el-input v-model="formData.password" type="password" />
      </el-form-item>
      <el-form-item prop="checkPassword" label="再次确认密码">
        <el-input v-model="formData.checkPassword" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    // 自定义校验函数
    const checkPassword = (rule, val, callback) => {
      const isValid = val === this.formData.password
      if (isValid) {
        callback()
      } else {
        callback(new Error('再次输入密码必须相同'))
      }
    }
    return {
      formData: {
        mobile: '',
        password: '',
        checkPassword: ''
      },
      rules: {
        mobile: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确额手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 16, min: 6, message: '长度在6-16之间', trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { validator: checkPassword, trigger: 'blur' }
        ]
      }
    }
  }
}
</script>
